package com.ydd.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.constant.MessageConstant;
import com.ydd.dto.SysMenuAddDTO;
import com.ydd.dto.SysMenuDto;
import com.ydd.entity.SysMenu;
import com.ydd.entity.YddMenuTwo;
import com.ydd.mapper.SysMenuMapper;
import com.ydd.result.Result;
import com.ydd.service.SysMenuService;
import com.ydd.vo.YddMenuAllDataVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author zhangxiaojun
 * @since 2023-05-14
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
@Autowired
private SysMenuMapper sysMenuMapper;


    @Override
    public List<SysMenuDto> allData() {
        //1.拿到父级菜单的评判标准是pid=0&&type=1
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getPid,0).eq(SysMenu::getType,1);
        List<SysMenu> sysMenus = this.list(wrapper);
        List<SysMenuDto> sysMenuDtoList = new ArrayList<>();
        for (SysMenu sysMenu : sysMenus) {
            SysMenuDto sysMenuDto = new SysMenuDto();
            BeanUtils.copyProperties(sysMenu,sysMenuDto);
            //2.拿到次级菜单
            LambdaQueryWrapper<SysMenu> Childwrapper = new LambdaQueryWrapper<>();
            Childwrapper.eq(SysMenu::getPid,sysMenu.getId()).eq(SysMenu::getType,1);
            List<SysMenu> sysMenus1 = this.list(Childwrapper);
            List<SysMenuDto> childtoList = new ArrayList<>();
            for (SysMenu menu : sysMenus1) {
                SysMenuDto menuDto = new SysMenuDto();
                BeanUtils.copyProperties(menu,menuDto);
                //3.拿到孙级菜单
                LambdaQueryWrapper<SysMenu> pper = new LambdaQueryWrapper<>();
                pper.eq(SysMenu::getPid,menu.getId()).eq(SysMenu::getType,2);
                List<SysMenu> list = this.list(pper);
                List<SysMenuDto> sysMenuDtolist = new ArrayList<>();
                for (SysMenu sysMenu1 : list) {
                    SysMenuDto sysMenuDto1 = new SysMenuDto();
                    BeanUtils.copyProperties(sysMenu1,sysMenuDto1);
                    sysMenuDtolist.add(sysMenuDto1);
                }
                menuDto.setChildren(sysMenuDtolist);
                childtoList.add(menuDto);

            }
            sysMenuDto.setChildren(childtoList);
            sysMenuDtoList.add(sysMenuDto);
        }
        return sysMenuDtoList;
    }

    //删除
    @Override
    public void deleteMenu(Integer id) {
        sysMenuMapper.deleteMenu(id);
    }

    //菜单添加
    @Override
    public void add(SysMenuAddDTO dto) {
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(dto,sysMenu);
        sysMenu.setCreateDate(LocalDateTime.now());
        sysMenu.setUpdateDate(LocalDateTime.now());
        sysMenu.setDelFlag(0);
        sysMenu.setDefaultData("1");
        sysMenuMapper.add(sysMenu);
    }

}
